// miniprogram/pages/my/member/index.js
import * as R from 'ramda';
import U from "../../../utils/index";
import config from '../../../config/index';
import { Reader } from "../../../utils/vendor/ramda-fantasy"
const globalData = getApp().globalData;

// 根据权限设置参数
const setParam = role => Reader(
  that => R.ifElse(
    () => R.equals(0, role),
    () => that.setData({
      tabDatas: [{
        name: '总公司',
        param: { is_agent: 0 }
      }, {
        name: '代理商',
        param: { is_agent: 1 }
      }]
    }),
    () => that.setData({
      tabDatas: [{
        name: '代理公司成员',
        param: {}
      }]
    })
  )()
);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    roleTypeNames: config.roleTypes,
    index: 0,
    params: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    R.compose(
      () => this.turnPage(this.data.index),
      () => this.data.tabDatas.map((value, index) => {
        this.setData({
          [ 'currentPageData.pageData' + index ]: [],
          [ 'currentPageData.pageNum' + index ]: 1,
          [ 'currentPageData.isLastPage' + index ]: false,
          [ 'currentPageData.isShowPageLoading' + index ]: false,
        })
      }),
      U.run(this),
      setParam
    )(globalData.userInfo.role);
  },
  onClickTab(e) {
    R.compose(
      R.when(
        () => this.data.currentPageData['pageNum' + this.data.index] === 1,
        () => this.turnPage(this.data.index)
      ),
      () => this.setData({ index: e.detail.index }),
      () => wx.pageScrollTo({
        scrollTop: 0,
        duration: 0,
      })
    )();
  },
  turnPage(index) {
    U.turnPage(() => U.reqPost({
      url: '/user/user_list',
      data: {
        ...this.data.tabDatas[index].param,
        page_num: this.data.currentPageData['pageNum' + index],
        page_size: config.defaultPageSize
      }
    }), {
      pageData: 'pageData' + index,
      pageNum: 'pageNum' + index,
      isLastPage: 'isLastPage' + index,
      isShowPageLoading: 'isShowPageLoading' + index,
    }).run(this);
  }
})
